package com.changpay. zywy.ui.fragment.main

import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.RankAdapter
import com.changpay.zywy.base.BaseMvpFragment
import com.changpay.zywy.ext.showToast

import com.changpay.zywy.mvp.contract.RankContract
import com.changpay.zywy.bean.RankBean
import com.changpay.zywy.mvp.presenter.RankPresenter
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.fragment_refresh_layout.*


class RankFragment : BaseMvpFragment<RankContract.View, RankContract.Presenter>(), RankContract.View {
    private var position: Int = 0  // 当前viewPage的位置
    private lateinit var type: String
    private lateinit var headerView: View
    private var tv_rownum: TextView? = null
    private var tv_phone: TextView? = null
    private var tv_amount: TextView? = null
    private var tv_unit_one: TextView? = null
    private var tv_unit_two: TextView? = null


    companion object {
        fun getInstance(instanceType: Int): RankFragment {
            val bundle = Bundle()
            bundle.putInt("position", instanceType)
            val fragment = RankFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun showData(data: RankBean) {
        val listData = data.list
        var type = 1
        listData!!.forEach {
            it.mitemType = type
            type = if (type == 1) 2 else 1
        }
        datas.addAll(listData)
        mAdapter.notifyDataSetChanged()

        if (position==1)
        {
            tv_unit_one?.text  = "元"
            tv_unit_two?.text  = "元"
        }else
        {
            tv_unit_one?.text  = "台"
            tv_unit_two?.text  = "台"
        }
        tv_rownum!!.text = data.my_ranking?.rownum.toString()
        tv_phone!!.text = data.my_ranking?.lg_member_name.toString()
        tv_amount!!.text = data.my_ranking?.amount.toString()
    }

    override fun createPresenter(): RankContract.Presenter = RankPresenter()

    override fun attachLayoutRes(): Int = R.layout.fragment_refresh_layout

    override fun lazyLoad() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["type"] = type
        mPresenter?.getRank(param)
    }

    override fun initView(view: View) {
        super.initView(view)
        position = arguments?.getInt("position") ?: 0
        type = if (position == 1) "trading" else "activation"
        swipeRefreshLayout.isEnabled = false
        initRecyclerView()
    }

    /**
     * datas
     */
    private val datas = mutableListOf<RankBean.ListBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: RankAdapter by lazy {
        RankAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }

    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {
        showToast("更多结束")
//        todayTardAdapter.addData("")
//        todayTardAdapter.loadMoreComplete()
        //        val page = homeAdapter.data.size / 20
//        mPresenter?.requestArticles(page)
    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas.size != 0) {
            val data = datas[position]
//            Intent(activity, ContentActivity::class.java).run {
//                putExtra(Constant.CONTENT_URL_KEY, data.link)
//                putExtra(Constant.CONTENT_TITLE_KEY, data.title)
//                putExtra(Constant.CONTENT_ID_KEY, data.id)
//                startActivity(this)
//            }
        }
    }

    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
        if (datas.size != 0) {
            val data = datas[position]
            when (view.id) {
//                    R.id.iv_like -> {
//
//                    }
            }
        }
    }


    private fun initRecyclerView() {
        headerView = layoutInflater.inflate(R.layout.item_rank_head, null)
        headerView.run {
            tv_unit_one = headerView.findViewById(R.id.tv_unit)
            tv_unit_two = headerView.findViewById(R.id.tv_unit_two)
            tv_rownum = headerView.findViewById(R.id.tv_rownum)
            tv_phone = headerView.findViewById(R.id.tv_phone)
            tv_amount = headerView.findViewById(R.id.tv_number)
        }
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this.onItemClickListener
            onItemChildClickListener = this@RankFragment.onItemChildClickListener
            setEmptyView(R.layout.fragment_empty_layout)
            addHeaderView(headerView)
        }
    }
}






